package com.mathdroid.gui.main;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.SwingWorker;

/**
 * Listens to AOptimizerFrames. Cancels the SwingWorker Task and disposes the
 * window.
 *
 * @param <T> First swing worker parameter.
 * @param <U> Second swing worker parameter.
 */
public final class OptimizerWindowListener<T, U> implements WindowListener {
	
	/**
	 * SwingWorker background task.
	 */
	private SwingWorker<T, U> task = null;
	
	/**
	 * The window.
	 */
	private JFrame frame = null;

	/**
	 * Create a new window listener.
	 * @param t SwingWorker task.
	 * @param f Parent frame.
	 */
	public OptimizerWindowListener(final SwingWorker<T, U> t, final JFrame f) {
		this.task = t;
		this.frame  = f;
	}

	@Override
	public void windowActivated(final WindowEvent e) { }

	@Override
	public void windowClosed(final WindowEvent e) { }

	@Override
	public void windowClosing(final WindowEvent e) {
		this.task.cancel(false);
		this.frame.dispose();
	}

	@Override
	public void windowDeactivated(final WindowEvent e) { }

	@Override
	public void windowDeiconified(final WindowEvent e) { }

	@Override
	public void windowIconified(final WindowEvent e) { }

	@Override
	public void windowOpened(final WindowEvent e) { }
}
